<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>quadtree_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_quadtree_test.js.html">quadtree_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.QuadTreeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.QuadTreeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.structs&#39;);
<a name="line19"></a>goog.require(&#39;goog.structs.QuadTree&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>function getTree() {
<a name="line23"></a>  var qt = new goog.structs.QuadTree(0, 0, 100, 100);
<a name="line24"></a>  qt.set(5, 20, &#39;Foo&#39;);
<a name="line25"></a>  qt.set(50, 32, &#39;Bar&#39;);
<a name="line26"></a>  qt.set(47, 96, &#39;Baz&#39;);
<a name="line27"></a>  qt.set(50, 50, &#39;Bing&#39;);
<a name="line28"></a>  qt.set(12, 0, &#39;Bong&#39;);
<a name="line29"></a>  return qt;
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function testGetCount() {
<a name="line33"></a>  var qt = getTree();
<a name="line34"></a>  assertEquals(&#39;Count should be 5&#39;, 5, qt.getCount());
<a name="line35"></a>  qt.remove(50, 32);
<a name="line36"></a>  assertEquals(&#39;Count should be 4&#39;, 4, qt.getCount());
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function testGetKeys() {
<a name="line40"></a>  var keys = getTree().getKeys();
<a name="line41"></a>  var keyString = keys.sort().join(&#39; &#39;);
<a name="line42"></a>  var expected = &#39;(12, 0) (47, 96) (5, 20) (50, 32) (50, 50)&#39;;
<a name="line43"></a>  assertEquals(&#39;Sorted keys should be &#39; + expected, expected, keyString);
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testGetValues() {
<a name="line47"></a>  var values = getTree().getValues();
<a name="line48"></a>  var valueString = values.sort().join(&#39;,&#39;);
<a name="line49"></a>  assertEquals(&#39;Sorted values should be Bar,Baz,Bing,Bong,Foo&#39;,
<a name="line50"></a>      &#39;Bar,Baz,Bing,Bong,Foo&#39;, valueString);
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>function testContains() {
<a name="line54"></a>  var qt = getTree();
<a name="line55"></a>  assertTrue(&#39;Should contain (5, 20)&#39;, qt.contains(5, 20));
<a name="line56"></a>  assertFalse(&#39;Should not contain (13, 13)&#39;, qt.contains(13, 13));
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testClear() {
<a name="line60"></a>  var qt = getTree();
<a name="line61"></a>  qt.clear();
<a name="line62"></a>  assertTrue(&#39;Tree should be empty&#39;, qt.isEmpty());
<a name="line63"></a>  assertFalse(&#39;Tree should not contain (5, 20)&#39;, qt.contains(5, 20));
<a name="line64"></a>}
<a name="line65"></a>
<a name="line66"></a>function testConstructor() {
<a name="line67"></a>  var qt = new goog.structs.QuadTree(-10, -5, 6, 12);
<a name="line68"></a>  var root = qt.getRootNode();
<a name="line69"></a>  assertEquals(&#39;X of root should be -10&#39;, -10, root.x);
<a name="line70"></a>  assertEquals(&#39;Y of root should be -5&#39;, -5, root.y);
<a name="line71"></a>  assertEquals(&#39;Width of root should be 16&#39;, 16, root.w);
<a name="line72"></a>  assertEquals(&#39;Height of root should be 17&#39;, 17, root.h);
<a name="line73"></a>  assertTrue(&#39;Tree should be empty&#39;, qt.isEmpty());
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testClone() {
<a name="line77"></a>  var qt = getTree().clone();
<a name="line78"></a>  assertFalse(&#39;Clone should not be empty&#39;, qt.isEmpty());
<a name="line79"></a>  assertTrue(&#39;Should contain (47, 96)&#39;, qt.contains(47, 96));
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testRemove() {
<a name="line83"></a>  var qt = getTree();
<a name="line84"></a>  assertEquals(&#39;(5, 20) should be removed&#39;, &#39;Foo&#39;, qt.remove(5, 20));
<a name="line85"></a>  assertEquals(&#39;(5, 20) should be removed&#39;, &#39;Bar&#39;, qt.remove(50, 32));
<a name="line86"></a>  assertEquals(&#39;(5, 20) should be removed&#39;, &#39;Baz&#39;, qt.remove(47, 96));
<a name="line87"></a>  assertEquals(&#39;(5, 20) should be removed&#39;, &#39;Bing&#39;, qt.remove(50, 50));
<a name="line88"></a>  assertEquals(&#39;(5, 20) should be removed&#39;, &#39;Bong&#39;, qt.remove(12, 0));
<a name="line89"></a>  assertNull(&#39;(6, 6) wasn\&#39;t there to remove&#39;, qt.remove(6, 6));
<a name="line90"></a>  assertTrue(&#39;Tree should be empty&#39;, qt.isEmpty());
<a name="line91"></a>  assertTreesChildrenAreNull(qt);
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>function testIsEmpty() {
<a name="line95"></a>  var qt = getTree();
<a name="line96"></a>  qt.clear();
<a name="line97"></a>  assertTrue(qt.isEmpty());
<a name="line98"></a>  assertEquals(&#39;Root should  be empty node&#39;,
<a name="line99"></a>      goog.structs.QuadTree.NodeType.EMPTY, qt.getRootNode().nodeType);
<a name="line100"></a>  assertTreesChildrenAreNull(qt);
<a name="line101"></a>}
<a name="line102"></a>
<a name="line103"></a>function testForEach() {
<a name="line104"></a>  var qt = getTree();
<a name="line105"></a>  var s = &#39;&#39;;
<a name="line106"></a>  goog.structs.forEach(qt, function(val, key, qt2) {
<a name="line107"></a>    assertNotUndefined(key);
<a name="line108"></a>    assertEquals(qt, qt2);
<a name="line109"></a>    s += key.x + &#39;,&#39; + key.y + &#39;=&#39; + val + &#39; &#39;;
<a name="line110"></a>  });
<a name="line111"></a>  assertEquals(&#39;50,32=Bar 50,50=Bing 47,96=Baz 5,20=Foo 12,0=Bong &#39;, s);
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>function testBalancing() {
<a name="line115"></a>  var qt = new goog.structs.QuadTree(0, 0, 100, 100);
<a name="line116"></a>  var root = qt.getRootNode();
<a name="line117"></a>
<a name="line118"></a>  // Add a point to the NW quadrant.
<a name="line119"></a>  qt.set(25, 25, &#39;first&#39;);
<a name="line120"></a>
<a name="line121"></a>  assertEquals(&#39;Root should be a leaf node.&#39;,
<a name="line122"></a>      goog.structs.QuadTree.NodeType.LEAF, root.nodeType);
<a name="line123"></a>  assertTreesChildrenAreNull(qt);
<a name="line124"></a>
<a name="line125"></a>  assertEquals(&#39;first&#39;, root.point.value);
<a name="line126"></a>
<a name="line127"></a>  // Add another point in the NW quadrant
<a name="line128"></a>  qt.set(25, 30, &#39;second&#39;);
<a name="line129"></a>
<a name="line130"></a>  assertEquals(&#39;Root should now be a pointer.&#39;,
<a name="line131"></a>      goog.structs.QuadTree.NodeType.POINTER, root.nodeType);
<a name="line132"></a>  assertNotNull(&#39;NE should be not be null&#39;, root.ne);
<a name="line133"></a>  assertNotNull(&#39;NW should be not be null&#39;, root.nw);
<a name="line134"></a>  assertNotNull(&#39;SE should be not be null&#39;, root.se);
<a name="line135"></a>  assertNotNull(&#39;SW should be not be null&#39;, root.sw);
<a name="line136"></a>  assertNull(root.point);
<a name="line137"></a>
<a name="line138"></a>  // Delete the second point.
<a name="line139"></a>  qt.remove(25, 30);
<a name="line140"></a>
<a name="line141"></a>  assertEquals(&#39;Root should have been rebalanced and be a leaf node.&#39;,
<a name="line142"></a>      goog.structs.QuadTree.NodeType.LEAF, root.nodeType);
<a name="line143"></a>  assertTreesChildrenAreNull(qt);
<a name="line144"></a>  assertEquals(&#39;first&#39;, root.point.value);
<a name="line145"></a>}
<a name="line146"></a>
<a name="line147"></a>function testTreeBounds() {
<a name="line148"></a>  var qt = getTree();
<a name="line149"></a>  assertFails(qt, qt.set, [-10, -10, 1]);
<a name="line150"></a>  assertFails(qt, qt.set, [-10, 10, 2]);
<a name="line151"></a>  assertFails(qt, qt.set, [10, -10, 3]);
<a name="line152"></a>  assertFails(qt, qt.set, [-10, 110, 4]);
<a name="line153"></a>  assertFails(qt, qt.set, [10, 130, 5]);
<a name="line154"></a>  assertFails(qt, qt.set, [110, -10, 6]);
<a name="line155"></a>  assertFails(qt, qt.set, [150, 14, 7]);
<a name="line156"></a>}
<a name="line157"></a>
<a name="line158"></a>// Helper functions
<a name="line159"></a>
<a name="line160"></a>function assertFails(context, fn, args) {
<a name="line161"></a>  assertThrows(&#39;Exception expected from &#39; + fn.toString() +
<a name="line162"></a>      &#39; with arguments &#39; + args,
<a name="line163"></a>      function() {
<a name="line164"></a>        fn.apply(context, args);
<a name="line165"></a>      });
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>function assertTreesChildrenAreNull(qt) {
<a name="line169"></a>  var root = qt.getRootNode();
<a name="line170"></a>  assertNull(&#39;NE should be null&#39;, root.ne);
<a name="line171"></a>  assertNull(&#39;NW should be null&#39;, root.nw);
<a name="line172"></a>  assertNull(&#39;SE should be null&#39;, root.se);
<a name="line173"></a>  assertNull(&#39;SW should be null&#39;, root.sw);
<a name="line174"></a>}
</pre>


</body>
</html>
